package com.csthink.web.interfaces;

import com.csthink.mq.payload.RocketMessage;
import com.csthink.web.infrastructure.annotations.IgnoreWebSecurity;
import com.csthink.web.infrastructure.mq.producer.RocketMQPublisherProperties;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

import static com.csthink.web.infrastructure.constant.AsyncMQConstants.*;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-07
 */
@Api(tags = "Test 模块")
@Slf4j
@RequestMapping("rocketmq-producer")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
public class RocketMQTestController {

    private final RocketMQTemplate rocketMQTemplate;

    private final RocketMQPublisherProperties publisherProperties;

    @PreAuthorize("@decider.allowUserEdit()")
    @GetMapping("/string-test")
    public void sendStringTest() {
        Message<String> msg = MessageBuilder
                .withPayload("Hello,RocketMQ")
                .build();

        String destination = publisherProperties.getTopicPracticePractice() + ":" + ROCKETMQ_TAG_ADD;
        rocketMQTemplate.send(destination, msg);
        log.info("#SendStringTest Send to dest: {}, msg={} done#,", destination, msg);
    }

    @PreAuthorize("@decider.allowInboxEdit()")
    @GetMapping("/entity-test")
    public void sendEntityTest() {
        RocketMessage msg = RocketMessage.builder()
                .id(UUID.randomUUID().toString())
                .operation("update")
                .localDate(LocalDate.now())
                .localDateTime(LocalDateTime.now())
                .build();

        String destination = publisherProperties.getTopicPracticePractice() + ":"
                + ROCKETMQ_TAG_UPDATE;

        rocketMQTemplate.convertAndSend(destination, msg);
        log.info("#SendEntityTest Send to dest: {}, msg={} success#", destination, msg);
    }

    @IgnoreWebSecurity
    @GetMapping("/entity-test2")
    public void sendEntityTest2() {
        RocketMessage msg = RocketMessage.builder()
                .id(UUID.randomUUID().toString())
                .operation("delete")
                .localDate(LocalDate.now())
                .localDateTime(LocalDateTime.now())
                .build();

        String destination = publisherProperties.getTopicPracticePractice() + ":" + ROCKETMQ_TAG_DELETE;
        rocketMQTemplate.convertAndSend(destination, msg);
        log.info("#SendEntityTest2 Send to dest: {}, msg={} success#", destination, msg);
    }
}
